import time
import sys
import json
import io
# 设置sys.stdout的编码为UTF-8
sys.stdout.reconfigure(encoding='utf-8')
# 创建一个新的输出流，并设置编码为UTF-8
#sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
# 将 当且路径 添加到path中
sys.path.append(r'.')
from DrissionPage._pages.chromium_tab import ChromiumTab


def click_base_card(tab: ChromiumTab):
    base_card = tab.ele('#base-card')
    base_card.click()


def item_title(tab: ChromiumTab, itemBean):
    struct_title_div = tab.ele('#struct-title')  # 宝贝标题 横排
    title_input = struct_title_div.ele('tag:input@@placeholder=最多允许输入30个汉字（60字符）')
    title_input.focus()  # 获取焦点
    title_input.click()  # 点击
    title_input.input(itemBean['titile'])
    time.sleep(1)
    tab.ele('#struct-catProp')  # 类目属性


def item_cat_prop(tab: ChromiumTab, itemBean):
    propBeans = itemBean['propBeans']

    more_span = tab.ele('tag:span@@text()=展开补充更多信息')
    if more_span:
        more_span.click()
        time.sleep(1)

    struct_catProp_div = tab.ele('#struct-catProp')  # 类目属性
    # 所有属性
    sell_catProp_items = struct_catProp_div.eles('tag:div@@class=sell-itemProp-item-common sell-itemProp-item')

    for catProp_div in sell_catProp_items:
        # 类目属性 左边: 品牌
        itemProp_name_div = catProp_div.ele('tag:div@@class=left-wrap')
        property_name = itemProp_name_div.text
        property_value = None
        for bean in propBeans:
            if property_name == bean['propName']:
                property_value = bean['propValue']
                print(property_name + '=======' + property_value)
        if property_value is None:
            # 没有属性值
            continue

        itemProp_name_div.click()  # 失去焦点
        itemProp_input_div = catProp_div.ele('tag:div@@class=sell-itemProp-struct')
        select_input = itemProp_input_div.ele('tag:input')
        need_search = False
        if select_input:  # 找到选择框
            placeholder = select_input.attr('placeholder')
            if '请选择' == placeholder:
                select_input.click()
                need_search = True
            elif '数字' == placeholder:
                select_input.click()
                time.sleep(1)
                select_input.input(int(property_value))
            else:
                select_input.click()
                select_input.input(property_value)

        if not need_search:
            continue
        time.sleep(1)
        # 获取 所有的下拉列表框
        search_class_style = 'next-overlay-inner next-select-popup-wrap sell-o-select-popup-overlay'
        pin_search_reslut_divs = tab.eles('tag:div@@class=' + search_class_style)
        time.sleep(1)
        # 每点击一次, 就会创建新的div, 只操作自己的div
        for pin_search_reslut_div in pin_search_reslut_divs:
            # 没有隐藏的框 (其他div隐藏)
            aria_hidden = pin_search_reslut_div.attr('aria-hidden')
            if 'false' == aria_hidden: #  必须是fals , 模块是关闭的
                # 搜索框
                infor_search_input = pin_search_reslut_div.ele("tag:input")
                if infor_search_input:
                    infor_search_input.click() # 输入框获取焦点
                    infor_search_input.input(property_value)
                    time.sleep(1)
                # 下拉列表
                time.sleep(1)
                info_contents = pin_search_reslut_div.eles('tag:div@@class=info-content')
                # 保存所有参数到字典中, 后续保存到json文件中
                for infor_div in info_contents:
                    if property_value and infor_div.text == property_value:
                        infor_div.click()
                time.sleep(1)
        itemProp_name_div.click()  # 失去焦点




#   基础信息
def base_card(tab: ChromiumTab, itemBean):
    click_base_card(tab)  # 获取焦点
    item_title(tab, itemBean)  # 宝贝标题
    item_cat_prop(tab, itemBean)  # 类目属性
